package monster.comm.util;

import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import monster.comm.exception.InvalidParameterException;
import monster.comm.exception.UniqueNeedException;

import sun.jdbc.rowset.CachedRowSet;

/**
 * 工具类
 * @author wanghong
 * 2012-12-20
 * Tool.java
 * 
 */
public class Tool {
	/**
	 * 把传递过来的参数转化陈form隐藏元素
	 * @param request
	 * @return
	 */
	public static String creatFormHiddenElement(HttpServletRequest request){
		StringBuffer sb=new StringBuffer();
		Enumeration en=request.getParameterNames();
		String pName="";
		String pValue="";
		while(en.hasMoreElements()){
			pName=(String)en.nextElement();
			pValue=Tool.getRequest(pName, request);
			sb.append("<input type='hidden' name='"+pName+"' value='"+pValue+"'>\n");
			
		}
		return sb.toString();
	}
	public static String getParams(HttpServletRequest request){
		Enumeration en=request.getParameterNames();
		String s="";
		String pName="";
		String pValue="";
		int i=0;
		while(en.hasMoreElements()){
			pName=(String)en.nextElement();
			if(i!=0){
				s+="&";
			}
			if(i==0){
				s+="?";
			}
			s+=pName+"=";
			pValue=request.getParameter(pName);
			if(pValue==null)pValue="";
			s+=pValue;
			i++;
			
		}
		return s;
	}
	
	/**
     * @remark	得到上个页面的URL
     * @author shoopman
     * @pram	request
     * @return	登陆后返回的URL
     * */
    public static String getBackURI(HttpServletRequest request)
    {
    	String backUrl = "";		//登陆后需返回的URL
    	 String queryString = request.getQueryString();
		 String requestUri="";
		 if(request.getRequestURL()!=null){
			 requestUri=request.getRequestURL().toString();
		 }
		 String refererUrl = request.getHeader("Referer");	
		 if(request.getMethod().toUpperCase().equals("GET")){
			 if(null == queryString || "".equals(queryString))
				 backUrl = requestUri;
			 else
				 backUrl = requestUri + "?" + queryString;
		 }
		 else{
			 backUrl = refererUrl;
		 }
		 if(backUrl!=null && backUrl.trim().length()>7)
			 backUrl=backUrl.substring(7);
		 int len=backUrl.indexOf("/");
		 if(len!=-1)
			 backUrl=backUrl.substring(len);
		 backUrl=Tool.getStr(backUrl);
    	return backUrl;
    }
	
	 /**
     * @remark	得到上个页面的URL
     * @author shoopman
     * @pram	request
     * @return	登陆后返回的URL
     * */
    public static String getBackUrl(HttpServletRequest request)
    {
    	String backUrl = "";		//登陆后需返回的URL
    	 String queryString = request.getQueryString();
		 String requestUri = request.getRequestURL().toString();
		 String refererUrl = request.getHeader("Referer");	
		 
		 if(request.getMethod().toUpperCase().equals("GET")){
			 if(null == queryString || "".equals(queryString))
				 backUrl = requestUri;
			 else
				 backUrl = requestUri + "?" + queryString;
		 }
		 else{
			 backUrl = refererUrl;
		 }
		 System.out.println(backUrl);
    	return backUrl;
    }
    
    /**
	 * 一个字符串有null的转化为""
	 * 
	 * @param str
	 * @return
	 */
	public static String changeNull(String str) {
		if (str == null)
			return "";
		return str.trim();
	}

	/**
	 * 把一个字符串有空字符串的转化为null
	 * 
	 * @param str
	 * @return
	 */
	public static String changeEmpty(String str) {
		if (str == null)
			return str;
		else {
			if ("".equals(str) || "".equals(str.trim()))
				return null;
			else
				return str.trim();
		}

	}

	/**
	 * 把一个request里面的参数值转化成vo对象
	 * 
	 * @param request
	 * @param vo
	 *            要赋值的对象
	 * @return 已经赋值的对象
	 */
	public static Object requestToVo(
			javax.servlet.http.HttpServletRequest request, Object vo)
			throws Exception {
		HashMap map = new HashMap();
		Enumeration names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			map.put(name, request.getParameterValues(name));
		}
		org.apache.commons.beanutils.BeanUtils.populate(vo, map);

		return vo;
	}

	/**
	 * 在jsp显示一个字段的时候，避免null打印出来
	 * 
	 * @param crs
	 * @param FieldName
	 *            crs的字段名
	 * @return
	 * @throws Exception
	 */
	public static String getRsValue(CachedRowSet crs, String FieldName)
			throws Exception {
		String str = crs.getString(FieldName);
		if (crs.wasNull())
			str = "";
		return str;
	}

	

	/**
	 * 字符串替换
	 * 
	 * @param src
	 *            源字符串，要替换的字符串
	 * @param find
	 *            要被替换的字符串
	 * @param rep
	 *            要替换成的字符串
	 * @return 替换完成后的字符串
	 */
	public static String replace(String src, String find, String rep) {
		String s = "";
		int findLen = find.length();

		int b = 0, ob = 0;

		b = src.indexOf(find, b);
		while (b != -1) {
			s = s + src.substring(ob, b);
			s = s + rep;
			b = b + findLen;
			ob = b;
			b = src.indexOf(find, b);
		}
		s = s + src.substring(ob, src.length());

		return s;
	}

	/**
	 * 中文转换
	 * 
	 * @param str
	 * @return String
	 */
	public static String getStr(String str) {

		if (str == null)
			return null;
		if (str.trim().equals("null"))
			return "";
		try {
			return new String(str.getBytes("ISO8859-1"), "utf-8");
		} catch (Exception e) {
			System.err.println(e.getMessage());
			return "转换时出错";
		}

	}

	/**
	 * 转化成日期类型
	 * 
	 * @param gs
	 * @return String
	 */
	public static String getDate(String gs) {
		Calendar tmpc = Calendar.getInstance();
		int n1 = tmpc.get(Calendar.YEAR);
		int n2 = tmpc.get(Calendar.MONTH) + 1;
		String s2 = n2 + "";
		int n3 = tmpc.get(Calendar.DATE);
		String s3 = n3 + "";
		if (n2 < 10) {
			s2 = "0" + s2;
		}
		if (n3 < 10) {
			s3 = "0" + n3;
		}
		return n1 + gs + s2 + gs + s3;

	}

	
	public static String getRequest(String param,javax.servlet.http.HttpServletRequest request){
		String ret="";
		if(param==null || param.trim().length()==0)
			return ret;
		ret=request.getParameter(param);
		if(ret==null)ret="";
		ret=ret.trim();
		return ret;
	}
	/**
	 * 给日期加上一个天数后，得到一个新的日期
	 * 
	 * @param gs
	 * @return String
	 */
	public static String getnewDate(String olddate, int adddate, String gs) {

		int year = Integer.parseInt(olddate.substring(0, 4));
		int month = Integer.parseInt(olddate.substring(5, 7));
		int date = Integer.parseInt(olddate.substring(8, 10));

		GregorianCalendar mortgage = new GregorianCalendar(year, month - 1,
				date);
		mortgage.add(Calendar.DAY_OF_MONTH, adddate);
		java.util.Date d = mortgage.getTime();
		DateFormat df = DateFormat.getDateInstance();

		String nowdate = df.format(d);
		String[] s = nowdate.split(gs);
		String s0 = s[0];
		String s1 = s[1];
		String s2 = s[2];
		if (Integer.parseInt(s1) < 10)
			s1 = "0" + s1;
		if (Integer.parseInt(s2) < 10)
			s2 = "0" + s2;
		return s0 + gs + s1 + gs + s2;

	}
	
	/**
	 * 给日期加上一个年数后，得到一个新的日期
	 * 
	 * @param gs
	 * @return String
	 */
	public static String getnewDateAddYeay(String olddate, int addyear, String gs) {

		int year = Integer.parseInt(olddate.substring(0, 4));
		int month = Integer.parseInt(olddate.substring(5, 7));
		int date = Integer.parseInt(olddate.substring(8, 10));

		GregorianCalendar mortgage = new GregorianCalendar(year, month - 1,
				date);
		mortgage.add(Calendar.YEAR, addyear);
		java.util.Date d = mortgage.getTime();
		DateFormat df = DateFormat.getDateInstance();

		String nowdate = df.format(d);
		String[] s = nowdate.split(gs);
		String s0 = s[0];
		String s1 = s[1];
		String s2 = s[2];
		if (Integer.parseInt(s1) < 10)
			s1 = "0" + s1;
		if (Integer.parseInt(s2) < 10)
			s2 = "0" + s2;
		return s0 + gs + s1 + gs + s2;

	}
	

	/**
	 * 转化成日期类型
	 * 
	 * @param gs
	 *            "-" "/"
	 * @return 2005-5-5 11:12:13
	 */
	public static String getDateTime(String gs) {
		Properties properties = System.getProperties();
		properties.setProperty("user.timezone", "GMT+8");
		System.setProperties(properties);
		Calendar tmpc = Calendar.getInstance();
		int n1 = tmpc.get(Calendar.YEAR);
		int n2 = tmpc.get(Calendar.MONTH) + 1;
		String s2 = n2 + "";
		int n3 = tmpc.get(Calendar.DATE);
		String s3 = n3 + "";
		int n4 = tmpc.get(Calendar.HOUR_OF_DAY);
		String s4 = n4 + "";
		int n5 = tmpc.get(Calendar.MINUTE);
		String s5 = n5 + "";
		int n6 = tmpc.get(Calendar.SECOND);
		String s6 = n6 + "";
		if (n2 < 10) {
			s2 = "0" + s2;
		}
		if (n3 < 10) {
			s3 = "0" + s3;
		}
		if (n4 < 10) {
			s4 = "0" + s4;
		}
		if (n5 < 10) {
			s5 = "0" + s5;
		}
		if (n6 < 10) {
			s6 = "0" + s6;
		}

		/*
		 * System.out.println("Tool.getDateTime.得到当前时间:"+n1+gs+s2+gs+s3+"
		 * "+s4+":" +s5+":"+s6);
		 */
		return n1 + gs + s2 + gs + s3 + " " + s4 + ":" + s5 + ":" + s6;

	}

	/**
	 * 当前时间增加一个分钟
	 * 
	 * @param gs
	 *            "-" "/"
	 * @param addMin
	 * @return 2005-5-5 11:12:13
	 */
	public static String TimeAdd(String gs, int addMin) {
		Calendar tmpc = Calendar.getInstance();

		int n1 = tmpc.get(Calendar.YEAR);
		int n2 = tmpc.get(Calendar.MONTH) + 1;
		String s2 = n2 + "";
		int n3 = tmpc.get(Calendar.DATE);
		String s3 = n3 + "";
		int n4 = tmpc.get(Calendar.HOUR_OF_DAY);
		String s4 = n4 + "";
		int n5 = tmpc.get(Calendar.MINUTE) + addMin;
		String s5 = n5 + "";
		int n6 = tmpc.get(Calendar.SECOND);
		String s6 = n6 + "";
		if (n2 < 10) {
			s2 = "0" + s2;
		}
		if (n3 < 10) {
			s3 = "0" + s3;
		}
		if (n4 < 10) {
			s4 = "0" + s4;
		}
		if (n5 < 10) {
			s5 = "0" + s5;
		}
		if (n6 < 10) {
			s6 = "0" + s6;
		}

		/*
		 * System.out.println("Tool.TimeAdd.得到当前时间几分钟以后的时间:"+n1+gs+s2+gs+s3+"
		 * "+s4 +":"+s5+":"+s6);
		 */
		return n1 + gs + s2 + gs + s3 + " " + s4 + ":" + s5 + ":" + s6;

	}
	/**
	 * 得到岁数
	 * @param birth
	 * @return
	 */
	public static String  getAge(String birth){
		String ret="";
		if(isNull(birth).length()==0)
			return ret;
		String now_date=Tool.getDate("-");
		String s1=birth.substring(0,birth.indexOf("-"));
		String s2=now_date.substring(0,now_date.indexOf("-"));
		int sub=Integer.parseInt(s2)-Integer.parseInt(s1);
		return sub+"";
	}

	public static String isNull(Object s) {
		if (s == null)
			s = "";
		
		return s.toString().trim();
	}

	public static String toNull(String s) {
		if (s == "")
			return null;
		return s;
	}

	/**
	 * 这个函数的意图是获得web下配置的文件
	 * 
	 * @param filePath
	 *            String 注意 这个文件一定要放在WEB-INF/classes/下面，此为根eg:/test/tt.txt
	 *            就是WEB-INF/classes/test/tt.txt
	 * @throws Exception
	 * @return File
	 */
	public java.io.File getFile(String filePath) throws Exception {
		java.net.URL is = this.getClass().getResource(filePath);
		if (is == null) {
			throw new Exception("文件 " + filePath + " 不存在");
		}
		File file = new File(is.getFile());
		if (file == null) {
			throw new Exception("文件 " + filePath + " 不存在");
		}
		return file;
	}

	/**
	 * 两个javaBean值的交互
	 * 
	 * @param toObj
	 *            Object 赋予值的Bean
	 * @param fromObj
	 *            Object 有值的Bean
	 * @throws Exception
	 */

	public static void copyPropInObjects(Object toObj, Object fromObj)
			throws Exception {
		try {
			org.apache.commons.beanutils.PropertyUtils.copyProperties(toObj,
					fromObj);
		} catch (Exception e) {
			System.out.println("对象值交换失败：" + e.toString());
			throw new Exception("对象值交换失败：" + e.toString());
		}

	}

	/**
	 * 得到现在时间
	 * 
	 * @return long
	 */
	public static long getNowTime() {
		return new java.util.Date().getTime();
	}

	public static String replaceChar(String source) {

		String s1 = "";

		java.util.StringTokenizer st = new java.util.StringTokenizer(source,
				"`");
		while (st.hasMoreTokens()) {

			s1 += st.nextToken();
			if (st.hasMoreTokens()) {
				s1 += "1";
			}

		}

		return s1;
	}

	
	/**
	 * 判断一个字符串不能为"" 和null
	 * 
	 * @param s
	 * @return true
	 */
	public static boolean haveVal(String s) {
		String temp = s;
		if (temp == null)
			temp = "";
		if (temp.trim().equals(""))
			return false;
		return true;

	}

	/**
	 * 一个字符串没有值就出现例外
	 * 
	 * @param strV
	 * @param name
	 * @throws Exception
	 */
	public static void noValException(String strV, String name)
			throws Exception {
		if (strV == null || strV.trim().length() == 0) {
			throw new Exception(name + " 不能为null 或者空");
		}
	}

	public static String addDate(String strDate, int days) {
		String newDate = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			java.util.Date date = df.parse(strDate);
			Calendar canlendar = Calendar.getInstance();
			canlendar.setTime(date);
			canlendar.add(Calendar.DATE, days);
			date = canlendar.getTime();
			newDate = df.format(date);
		} catch (Exception e) {
			newDate = "0000-00-00";
		}
		return newDate;

	}

	public static String addMonth(String strDate, int month) {
		String newDate = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			java.util.Date date = df.parse(strDate);
			Calendar canlendar = Calendar.getInstance();
			canlendar.setTime(date);
			canlendar.add(Calendar.MONTH, month);
			date = canlendar.getTime();
			newDate = df.format(date);
		} catch (Exception e) {
			newDate = "0000-00-00";
		}
		return newDate;

	}

	public static String addDateTime(String strDate, int days) {
		String newDate = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			java.util.Date date = df.parse(strDate);
			Calendar canlendar = Calendar.getInstance();
			canlendar.setTime(date);
			canlendar.add(Calendar.DATE, days);
			date = canlendar.getTime();
			newDate = df.format(date);
		} catch (Exception e) {
			newDate = "0000-00-00";
		}
		return newDate;

	}

	/**
	 * 在一个时间上增加了几分钟
	 * 
	 * @param strDate
	 * @param min
	 * @return
	 */
	public static String addDateTime2(String strDate, int min) {
		String newDate = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			java.util.Date date = df.parse(strDate);
			Calendar canlendar = Calendar.getInstance();
			canlendar.setTime(date);
			canlendar.add(Calendar.MINUTE, min);
			date = canlendar.getTime();
			newDate = df.format(date);
		} catch (Exception e) {
			newDate = "0000-00-00";
		}
		return newDate;

	}

	




	/**
	 * @remark
	 * @author jzk
	 * @date 2006-6-19
	 * @return yyyy-mm
	 */
	public static String getLastMonth() {// 可以用addDateTime(String, int)替代

		String year = "";
		String month = "";
		Calendar calendar = new GregorianCalendar();
		int tempMonth = calendar.get(Calendar.MONTH);
		int tempYear = calendar.get(Calendar.YEAR);
		if (tempMonth == 0) {
			year = (tempYear - 1) + "";
			month = "12";
		} else {
			year = tempYear + "";
			if (tempMonth < 10) {
				month = "0" + tempMonth;
			} else {
				month = tempMonth + "";
			}
		}
		return year + "-" + month;
	}

	/**
	 * 这个是BeanUtilsBean的扩展类，主要处理struts的情况 考虑客户端都是string要和pojo的数据转换 String
	 * 为""的要转化成null
	 * 
	 * @param dest
	 *            被赋值的对象
	 * @param orig
	 *            赋值对象
	 * @throws Exception
	 */
	public static void cloneValue(Object dest, Object orig) throws Exception {
		BeanUtilExp util = new BeanUtilExp();
		util.copyValue(dest, orig);

	}

	/**
	 * 得到删除多条记录的hql语句
	 * 
	 * @param tblName表名
	 * @param pkName
	 *            主键字段名
	 * @param pks
	 *            主键值如"1,2,3"
	 * @return Object[0]=hql如："delete test where id in(?,?,?)"
	 *         Object[1]=主键值数组如：Str[]=new String{"1,2,3"};
	 */
	public static Object[] getDelMorePKHQL(String tblName, String pkName,
			String pks) {
		Object[] str = new Object[2];
		if (pks == null || pks.trim().length() == 0) {
			str[0] = "";
			str[1] = "";
			return str;
		}

		String[] pklist = pks.split(SysConstant.PKLIST_SPLIT);
		String hql = "delete  " + tblName + "  where " + pkName + " in(";
		for (int i = 0; i < pklist.length; i++) {
			hql += "?,";
		}
		hql = hql.substring(0, hql.length() - 1);
		hql += ")";
		str[0] = hql;
		str[1] = pklist;
		return str;
	}

	

	
	/**
	 * 我们用url传递一个url的时候如果后面URL带多个参数，会导致系统无法接受，所以我们统一用?代替&
	 * 后台进行转化
	 * @param url
	 * @return
	 */
	public static String changeUrlParam(String url){
		String temp="";
		url=url.replace("?","`");
		String sz[]=url.split("`");
		for(int i=0;i<sz.length;i++){
		   String v="&";
		    if(i==0)
		        v="?";
		    temp+=sz[i]+v;
		   }
		    temp=temp.substring(0,temp.length()-1);
		   
		   
		  return temp;
	}
	
	/**
	 * 从request获取值,如果是null则转化成""
	 * @param request
	 * @param name 参数值
	 * @return
	 */
	public static String getValFormReq(javax.servlet.http.HttpServletRequest request,String name){
		String ret="";
		ret=request.getParameter(name);
		if(ret==null)ret="";
		ret=ret.trim();
		return ret;
	}
	
	/**
	 * 从request获取多个值,如果是null则转化成""
	 * @param request
	 * @param name 参数值
	 * @return
	 */
	public static String getValFormReqVals(javax.servlet.http.HttpServletRequest request,String name,int index) throws Exception{
		String ret="";
		try{
		String sz[]=request.getParameterValues(name);
		ret=sz[index];
		if(ret==null)ret="";
		ret=ret.trim();
		}
		catch(Exception e){
			throw new Exception("取值可能越界:"+e);
		}
		return ret;
	}
	/**
	 * 如果是null 或者""就返回false
	 * @param str
	 * @return
	 */
	public static boolean isEmptyVal(String str){
		boolean b=false;
		if(str==null)str="";
		if(str.trim().length()==0){
			b=true;
		}
		return b;
	}
	public static boolean isNotEmpty(String str){
		boolean b=true;
		if(str==null)str="";
		if(str.trim().length()==0){
			b=false;
		}
		return b;
	}
	/**
	 * 去掉textarea里面的换行符
	 * @param String1
	 * @return
	 */
	public static String dropLine(String String1){
		String temp = "";
		if (String1 == null)
			temp = "";
		else {
			int int2 = String1.length();
			StringBuffer StringBuffer3 = new StringBuffer();
			int int4;

			for (int4 = 0; int4 < int2; ++int4) {
				char char5 = String1.charAt(int4);

				switch (char5) {
				case 10://换行的符号
					StringBuffer3.append("`");
					break;
				default:
					StringBuffer3.append(char5);
					break;
				case 13:
					break;
				}
			}
			temp = StringBuffer3.toString();
		}
		return temp;
	}
	
	/**
	 * 把textarea输入的有换行的字打印出来换行
	 * @param String1
	 * @return
	 * @throws Exception
	 */
	public static String getRowsStr(String String1) throws Exception {

		String temp = "";
		if (String1 == null)
			temp = "";
		else {
			int int2 = String1.length();
			StringBuffer StringBuffer3 = new StringBuffer();
			int int4;

			for (int4 = 0; int4 < int2; ++int4) {
				char char5 = String1.charAt(int4);

				switch (char5) {
				case 62:
					StringBuffer3.append("&gt;");
					break;
				case 60:
					StringBuffer3.append("&lt;");
					break;
				case 38:
					StringBuffer3.append("&amp;");
					break;
				case 32:
					StringBuffer3.append("");
					break;
				case 10:
					StringBuffer3.append("<BR>");
					break;
				default:
					StringBuffer3.append(char5);
					break;
				case 13:
					break;
				}
			}
			temp = StringBuffer3.toString();
		}
		return temp;
	}
	
	/**
	 * 判断一个号码是否是手机号
	 * @param code
	 * @return
	 */
	public static  boolean isMobile(String code){
		boolean b=true;
		String t=Tool.isNull(code);
		String number="0123456789";
		if(t.length()!=11)
			return false;
		String s="";
		for(int i=0;i<t.length();i++){
			s=t.substring(i,i+1);
			if(number.indexOf(s)==-1)
				return false;
		}
		
		return b;
	}
	
	/**
	 * 把一个bean的属性值为null的转换为""字符串，方便打印
	 * @param obj
	 */
	public static void setBeanNullToEmpty(Object obj){
		BeanWrapper wap=new BeanWrapperImpl(obj);
		PropertyDescriptor[] props=wap.getPropertyDescriptors();//得到属性信息
		for (int i = 0; i < props.length; i++) {
		      String name = props[i].getName();
		      String cls_name=props[i].getPropertyType().toString();
		      if (!"class java.lang.String".equals(cls_name)) {
		        continue; // No point in trying to set an object's class
		      }
		      Object val=wap.getPropertyValue(name);
		      if(val==null)//如果为空则设置为""字符串
		    	  wap.setPropertyValue(name,"");
		}
	}
	
	/**
	 * 创建utf文件
	 * @param filePath
	 * @param content
	 * @throws Exception
	 */
	public static void createUtfFile(String filePath,String content)throws Exception{
		OutputStreamWriter out =new OutputStreamWriter(new FileOutputStream(filePath),"UTF-8");
		out.write(content);
		out.flush();
		out.close();

	}
	/**
	 * 从一个javabean里面获取值
	 * @param proName
	 * @param bean
	 * @return
	 */
	public static String  getValFromBean(String proName,Object bean){
		String ret="";
		  BeanWrapper wap=new BeanWrapperImpl(bean);
		 ret=(String) wap.getPropertyValue(proName);
		 if(ret==null)ret="";
		return ret;
	}
	
	/**
	 * 获得我饿web工程根目录的绝对路径
	 * @param request
	 * @return
	 */
	public static String  getWebRootPath(javax.servlet.http.HttpServletRequest request){
			String file_dir=request.getSession().getServletContext().getRealPath("/");//发布目录的绝对路径
		    file_dir=file_dir.replace("\\", "/");
		    file_dir=file_dir.substring(0,file_dir.length()-1);
		   return file_dir;
	}
	/**
	 * 创建文件夹
	 * @param path
	 */
	public static void createFile(String path) throws Exception{
		File file=new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
	}
	/**
	 * 删除文件
	 * 
	 * @param file
	 */
	public static String delefiles(String path)throws Exception{
		File file=new File(path);
		String ret="-1";
		if(file.exists()){
			ret=deleteFile(file);
		}
		return ret;
	}
	private static String deleteFile(File file)throws Exception{ 
		   String ret="0";
		   if(file.exists()){ 
		    if(file.isFile()){ 
		     file.delete(); 
		    }else if(file.isDirectory()){ 
		     File files[] = file.listFiles(); 
		     for(int i=0;i<files.length;i++){ 
		      deleteFile(files[i]); 
		     } 
		    } 
		    file.delete(); 
		   }else{ 
			   ret="-1";
		       System.out.println("所删除的文件不存在！"+'\n'); 
		   } 
		   return ret;
		} 


	
	/**
	 * 删除数字带0结尾的数据
	 * @param str
	 * @return
	 */
	public static String dropNero(String str){
		if(str==null)return "";
		str=str.trim();
		String ret="";
		int len=str.indexOf(".");
		if(len==-1)
			return str;
		String one=str.substring(0,len);
		String two=str.substring(len+1);//小数部分
		while(true){
			if(two.length()==0)
				break;
			String t=two.substring(two.length()-1);
			if(t.equals("0"))
               two=two.substring(0,two.length()-1);
            else
              break;
		}
		if(two.length()==0)
			ret=one;
		else
		 ret=one+"."+two;
		return ret;
	}
	
	/**
	   * 得到属性文件
	   * @param file filePath 绝对路径+文件名
	   * @return
	   * @throws Exception
	   */
	    public static Properties load(String filePath) throws Exception {
	    	// filePath="D:/java/Tomcat%206.0/webapps/ROOT/WEB-INF/classes/oldDb.properties";
	    	 filePath=filePath.replaceAll("%20", " ");
	        File path = new File(filePath);
	       
	        if (!path.exists() || path.isDirectory()) {
	            throw new IllegalArgumentException("failed to find the file:" + path.getAbsolutePath());
	        }
	        FileInputStream in = null;
	        Properties props = new Properties();
	        try {
	            in = new FileInputStream(path);
	            props.load(in);
	        } catch (Exception e) {
	            throw e;
	        } finally {
	           if(in!=null)in.close();
	        }
	        return props;
	    }
	    
	    /**
	     * 得到属性文件
	     * @param pro
	     * @param key
	     * @return
	     */
	    public static String getProByKey(Properties pro,String key){
	    	String ret=pro.getProperty(key);
	    	if(ret==null)ret="";
	    	return ret;
	    }
	    /**
	     * 获得本周开始日期
	     * @return
	     */
	   public static String getNowWeekStart(){
		   String ss="";
		    Calendar c = Calendar.getInstance();  
		    //c.setTime(new Date()); /**如果当前日期就不需要传入日期**/ 
		    int weekDay = c.get(Calendar.DAY_OF_WEEK)==1?8:c.get(Calendar.DAY_OF_WEEK);  
		    c.add(Calendar.DATE,Calendar.MONDAY - weekDay);  
		    Date start = c.getTime();  
		    c.add(Calendar.DATE, 6);  
		     ss=String.format("%1$tY-%1$tm-%1$td", start);
		   return ss;
	   }
	   /**
	    * 获得本周结束日期
	    * @return
	    */
	   public static String getNowWeekEnd(){
		   String ss="";
		    Calendar c = Calendar.getInstance();  
		    //c.setTime(new Date());  /**如果当前日期就不需要传入日期**/
		    int weekDay = c.get(Calendar.DAY_OF_WEEK)==1?8:c.get(Calendar.DAY_OF_WEEK);  
		    c.add(Calendar.DATE,Calendar.MONDAY - weekDay);  
		    c.add(Calendar.DATE, 6);  
		     Date end = c.getTime(); 
		     ss=String.format("%1$tY-%1$tm-%1$td", end);
		   return ss;
	   }
	    /**
	     * 将数组组合成字符串
	     * @param arg
	     * @param separator 分隔符
	     * @param 是否加单引号
	     * @return
	     */
	    public static String arrayTOString(String[] arg,String separator,boolean f){
			StringBuilder ss=new StringBuilder();
			int size = arg.length;
			for(int i=0;i<size;i++){
				if(!arg.equals("")){
					if(f){
						 ss.append(arg[i]);
					}else{
						 ss.append("'").append(arg[i]).append("'");
					}
                  
                   if(i<size-1){
                	   ss.append(separator);
                   }
				}
			}
			return ss.toString();
		}
	    
	    /**
	    *@Description:  参数空异常
	    *@param s
	    *@throws InvalidParameterException  
	    *@return void    
	    *@throws
	     */
	    public static void NullException(String s)throws InvalidParameterException{
	    	if(s==null)s="";
	    	if(s.trim().length()==0)
	    		throw  new InvalidParameterException("null异常");
	    }
	    
	    /**
		    *@Description:  参数空异常
		    *@param s
		    *@throws InvalidParameterException  
		    *@return void    
		    *@throws
		     */
		    public static void NullException(Object val,String desc)throws InvalidParameterException{
		    	if(val==null )
		    		throw  new InvalidParameterException(desc+":null异常");
		    	if(val instanceof String){
		    		String t=(String)val;
		    		if(t.trim().length()==0)
		    			throw  new InvalidParameterException(desc+":null异常");
		    	}
		    }
		    /**
		    *@Description: 抛出唯一检查异常
		    *@param s
		    *@throws InvalidParameterException  
		    *@return void   
		     */
		    public static void UniqueException(String s)throws UniqueNeedException{
		    		throw  new UniqueNeedException(s);
		    }
		    /**
			 * Assert that an array has no null elements.
			 * Note: Does not complain if the array is empty!
			 * @param array the array to check
			 * @param message the exception message to use if the assertion fails
			 * @throws InvalidParameterException if the object array contains a <code>null</code> element
			 */
			public static void noNullElements(Object[] array, String message)throws InvalidParameterException{
				if(array == null){
					throw new InvalidParameterException(message);
				}
				if (array != null) {
					for (int i = 0; i < array.length; i++) {
						if (array[i] == null) {
							throw new InvalidParameterException(message);
						}
					}
				}
			}
			/**
			 * Assert that an Iterable has no null elements.
			 * Note: Does not complain if the array is empty!
			 * @param array the array to check
			 * @param message the exception message to use if the assertion fails
			 * @throws InvalidParameterException if the object array contains a <code>null</code> element
			 */
			public static void noNullIterable(Iterable<?> iterable, String message)throws InvalidParameterException{
				if(iterable == null){
					throw new InvalidParameterException(message);
				}
				if(!iterable.iterator().hasNext()){
					throw new InvalidParameterException(message);
				}
				for (Iterator<?> it = iterable.iterator();it.hasNext();) {
					if (it.next() == null) {
						throw new InvalidParameterException(message);
					}
				}
				
			}
			
			
			/**
			 * 
			*@Description: 得到具体数据字典的js
			*@param dict_code 数据字典的编码，可以是多个，也可以是一个，多个用逗号隔离，如：ZD_001,ZD_001
			*@param ServletContext app  jsp application
			*@return  
			*@return String    
			*@throws
			 */
			public static String getDictJs(String dict_code,ServletContext app){
				String ret="<script language=\"javascript\">\n";
				List list=null;
				Option  option=null;
				ret+="var xt_dict=new Array();\n";
				String[] sz=dict_code.split(",");
				String temp="";
				for(int i=0;i<sz.length;i++){
					temp=sz[i].trim();
					ret+="\n xt_dict['"+temp+"']=new Array();\n";
				    list=(List)app.getAttribute(temp);
				    if(list !=null){
				    	for(int j=0;j<list.size();j++){
				    		option=(Option)list.get(j);
				    		ret+="xt_dict['"+temp+"']['"+option.getValue()+"']='"+option.getLabel()+"';";
				    	}
				    }
				}
				ret+="</script>\n";
				return ret;
			}
			
			public static void main(String args[]){
				
			}
}
